﻿using DevExpress.XtraEditors;
using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.NeonatalObservationTable
{
    public partial class AddNeonatalObservationTable : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        NursingFormRecord record = new NursingFormRecord();
        Tb_User LoginUser;   //员工信息
        PatientInfo Patientinfo;    //患者信息
        string Id = string.Empty;
        #endregion

        #region 构造函数
        public AddNeonatalObservationTable(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            //成员变量
            this.LoginUser = loginUser;
            this.Patientinfo = patientInfo;
            this.Id = id;
            BindControl();
            BindInfo(this.Id);
        }
        public AddNeonatalObservationTable(string[] loginUser, string[] patientInfo)
        {
            InitializeComponent();
            //成员变量
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Patientinfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.Id = string.Empty;
            BindControl();
            BindInfo(this.Id);
        }
        public AddNeonatalObservationTable(string[] loginUser, string[] patientInfo, string id)
        {
            InitializeComponent();
            //成员变量
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Patientinfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.Id = id;
            BindControl();
            BindInfo(this.Id);
        }
        #endregion

        #region 显示赋值
        public void BindInfo(string id)
        {
            text_QianMing.Text = LoginUser.UserName;     //签名获取当前登录工号者
            date_RiQi.DateTime = DateTime.Now;     //默认显示当前的值
            text_ShiJian.Text = DateTime.Now.ToString("HH:mm");

            if (string.IsNullOrEmpty(id)) { return; }
            string sql = string.Format("SELECT NursingFormData FROM NursingFormRecord(NOLOCK) WHERE Id='{0}'", id);
            var NursingFormData = SqlHelper.ExecuteScalar(sql).ToString();

            NeonatalObservation nobn = new NeonatalObservation();
            nobn = XmlUtil.XMLToObject<NeonatalObservation>(NursingFormData, Encoding.UTF8);
            if (!string.IsNullOrEmpty(nobn.RiQi))
                date_RiQi.DateTime = Convert.ToDateTime(nobn.RiQi);//日期
            text_ShiJian.Text = DateTimeHelper.HHmmssToHHmm(nobn.ShiJian);   //时间
            text_ChenJianHuLi.Text = nobn.ChenJianHuLi;         //晨间护理
            text_ChiNaiLiang.Text = nobn.ChiNaiLiang;           // 吃奶量
            text_JiaNai.Text = nobn.JiaNai;                     //加奶
            text_YuNai.Text = nobn.YuNai;                       //余奶
            text_MuRu.Text = nobn.MuRu;                         //母乳
            text_NiaoLiang.Text = nobn.NiaoLiang;               //尿量
            text_NiaoSe.Text = nobn.NiaoSe;                     //尿色
            text_Fen_Liang.Text = nobn.FenLiang;                //粪量
            text_Fen_Se.Text = nobn.FenSe;                      //粪色
            text_Fen_XingZhuang.Text = nobn.FenXingZhuang;      //粪性状
            text_WenXiang.Text = nobn.WenXiang;                 //温箱
            text_ShiDu.Text = nobn.ShiDu;                       //湿度
            text_HR.Text = nobn.HR;                             //HR
            text_R.Text = nobn.R;                               //R
            text_SPO.Text = nobn.SPO;                           //SPO
            text_BP.Text = nobn.BP;                             //BP
            text_LanGuangLiao.Text = nobn.LanGuangLiao;         //蓝光疗
            text_TuNaiLiang.Text = nobn.TuNaiLiang;             //吐奶量
            text_QianMing.Text = nobn.QianMing;                 //签名
        }
        #endregion

        #region 保存
        private void Btn_BaoCun_Click(object sender, EventArgs e)
        {
            //日期判断
            if (string.IsNullOrEmpty(date_RiQi.Text.Trim()))
                date_RiQi.DateTime = DateTime.Now;
            //时间判断
            if (!text_ShiJian.Text.Contains(":"))
            {
                text_ShiJian.Focus();
                MessageBox.Show("时间需HH:mm格式，或使用双击显示当前时间");
                return;
            }
            text_ShiJian.Text = DateTimeTran.TimeFillZero(text_ShiJian.Text);
            if (string.IsNullOrEmpty(text_ShiJian.Text)) text_ShiJian.Text = DateTime.Now.ToString("HH:mm:ss");

            NeonatalObservation nob = new NeonatalObservation();
            nob.RiQi = date_RiQi.DateTime.ToString("yyyy-MM-dd");   //日期
            nob.ShiJian = DateTimeTran.TimeFillZero(text_ShiJian.Text);    //时间
            nob.ChenJianHuLi = text_ChenJianHuLi.Text;         //晨间护理
            nob.ChiNaiLiang = text_ChiNaiLiang.Text;           //吃奶量
            nob.JiaNai = text_JiaNai.Text;                     //加奶
            nob.YuNai = text_YuNai.Text;                       //余奶
            nob.MuRu = text_MuRu.Text;                         //母乳
            nob.NiaoLiang = text_NiaoLiang.Text;               //尿量
            nob.NiaoSe = text_NiaoSe.Text;                     //尿色
            nob.FenLiang = text_Fen_Liang.Text;                //粪量
            nob.FenSe = text_Fen_Se.Text;                      //粪色
            nob.FenXingZhuang = text_Fen_XingZhuang.Text;      //粪性状
            nob.WenXiang = text_WenXiang.Text;                 //温箱
            nob.ShiDu = text_ShiDu.Text;                       //湿度
            nob.HR = text_HR.Text;                             //HR
            nob.R = text_R.Text;                               //R
            nob.SPO = text_SPO.Text;                           //SPO
            nob.BP = text_BP.Text;                             //BP
            nob.LanGuangLiao = text_LanGuangLiao.Text;         //蓝光疗
            nob.TuNaiLiang = text_TuNaiLiang.Text;             //吐奶量
            if (string.IsNullOrEmpty(Id))                      //签名
                nob.QianMing = LoginUser.UserName;
            else
                nob.QianMing = text_QianMing.Text;
            nob.FormId = (int)FormType.BabyBornObserve;       //表单Id
            nob.FormName = EnumHelper.GetDescription(FormType.BabyBornObserve);   //表单名字
            record.FormId = (int)FormType.BabyBornObserve;       //表单Id
            record.FormName = EnumHelper.GetDescription(FormType.BabyBornObserve);   //表单名字
            record.PatientId = Patientinfo.PATIENT_ID;     //住院号
            record.PatientName = Patientinfo.PATIENT_NAME;    //患者姓名
            record.BedNo = Patientinfo.BED_NO;     //床号
            record.VisitId = Patientinfo.VISIT_ID;         //住院次数
            record.DepartmentId = Patientinfo.DEPARTMENT_ID;     //当前科室ID
            record.DepartmentName = Patientinfo.DEPARTMENT_NAME;     //当前科室名字
            record.AreaId = Patientinfo.NURSE_CELL_CODE;   //当前患者病区ID
            record.AreaName = Patientinfo.NURSE_CELL_NAME;    //当前患者病区名称
            record.AssessmentDate = Convert.ToDateTime(nob.RiQi + " " + nob.ShiJian);
            record.Id = string.IsNullOrEmpty(Id) ? 0 : Convert.ToInt32(Id);
            record.NursingFormData = XmlUtil.ObjectToXML(nob, Encoding.UTF8);
            var ret = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));
            if (ret.Code == 1)
            {
                MessageBox.Show(ret.M);
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(ret.M);
            }
        }
        #endregion

        #region 双击显示当前时间
        private void text_ShiJian_DoubleClick(object sender, EventArgs e)
        {
            text_ShiJian.Text = DateTime.Now.ToString("HH:mm");
        }
        #endregion

        #region 设置 enter键当做Tab切换
        private void BindControl()
        {
            foreach (var c in panel5.Controls)
            {
                if (c is TextEdit)
                {
                    ((TextEdit)c).KeyUp += TextEdit_KeyUp;
                }
            }

            foreach (var c in groupControl1.Controls)
            {
                if (c is TextEdit)
                {
                    ((TextEdit)c).KeyUp += TextEdit_KeyUp;
                }
            }

            foreach (var c in groupControl5.Controls)
            {
                if (c is TextEdit)
                {
                    ((TextEdit)c).KeyUp += TextEdit_KeyUp;
                }
            }
            foreach (var c in groupControl4.Controls)
            {
                if (c is TextEdit)
                {
                    ((TextEdit)c).KeyUp += TextEdit_KeyUp;
                }
            }
            foreach (var c in groupControl3.Controls)
            {
                if (c is TextEdit)
                {
                    ((TextEdit)c).KeyUp += TextEdit_KeyUp;
                }
            }
            foreach (var c in groupControl2.Controls)
            {
                if (c is TextEdit)
                {
                    ((TextEdit)c).KeyUp += TextEdit_KeyUp;
                }
            }
            foreach (var c in groupControl6.Controls)
            {
                if (c is TextEdit)
                {
                    ((TextEdit)c).KeyUp += TextEdit_KeyUp;
                }
            }
            foreach (var c in groupControl8.Controls)
            {
                if (c is TextEdit)
                {
                    ((TextEdit)c).KeyUp += TextEdit_KeyUp;
                }
            }
        }
        /// <summary>
        /// 设置 enter键当做Tab切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextEdit_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }
        #endregion

        #region 取消
        private void Btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
    }
}
